package cz.incad.psh.pshassign.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import cz.incad.psh.pshassign.client.rpc.PSHService;
import cz.incad.psh.pshassign.client.rpc.PSHServiceAsync;
import static com.google.gwt.query.client.GQuery.$;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import static gwtquery.plugins.ui.Ui.Ui;

/**
 *
 * @author Alberto Hernandez
 */
public class AssignProgressBar extends Composite {

    private static AssignProgressBarUiBinder uiBinder = GWT.create(AssignProgressBarUiBinder.class);
    private final MainPage mainPage;

    interface AssignProgressBarUiBinder extends UiBinder<Widget, AssignProgressBar> {
    }
    PSHServiceAsync pshService = GWT.create(PSHService.class);
    Timer timer;
    @UiField
    HorizontalPanel content;
    
    @UiField
    FocusPanel fc;
    public boolean isRunning = false;

    public AssignProgressBar(MainPage mainPage) {
        initWidget(uiBinder.createAndBindUi(this));
        init();
        this.mainPage = mainPage;
    }

    private void init() {
        $("#progressbar").as(Ui).progressbar("{value:0}");
        timer = new Timer() {
            @Override
            public void run() {
                pshService.getCurrentAssign(new AsyncCallback() {
                    @Override
                    public void onFailure(Throwable caught) {
                        isRunning = false;
                        content.setVisible(isRunning);
                    }

                    @Override
                    public void onSuccess(Object result) {
                        int val = (Integer) result;
                        $("#progressbar>div.progress-label").html(Integer.toString(val) + "%");
                        $("#progressbar").as(Ui).progressbar("{value:" + val + "}");
                        content.setVisible(true);
                        timer.schedule(500);
                    }
                });

            }
        };
        fc.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                pshService.cancelAssign(new AsyncCallback<Void>() {
                    @Override
                    public void onFailure(Throwable caught) {
                        Window.alert("Error: " + caught.getLocalizedMessage());
                    }

                    @Override
                    public void onSuccess(Void result) {
                        Window.alert("Proccess will be cancelled");
                    }
                });
            }
        });

        pshService.getIsRunningAssign(new AsyncCallback() {
            @Override
            public void onFailure(Throwable caught) {
                isRunning = false;
                content.setVisible(isRunning);
            }

            @Override
            public void onSuccess(Object result) {
                isRunning = (Boolean) result;
                content.setVisible(isRunning);
            }
        });
    }

    public void sched() {
        timer.schedule(100);
    }
}
